package com.waterfairy.systemapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.waterfairy.systemapp2.R;


public class WebViewActivity extends AppCompatActivity {

    private static final String TAG = "webView";
    
    //登录地址
    private final String URL = "https://eos.douyin.com/livesite/login";

    WebView webView;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);

        findView();
        initView();
        initData();
    }

    private void initData() {

    }

    private void findView() {
        webView = findViewById(R.id.webview);
        imageView = findViewById(R.id.img_qr);
    }

    private void initView() {
        initWebView();
    }

    private void initWebView() {
        WebSettings webSettings = webView.getSettings();

        //!!!必须配置,否则无法交互
        webSettings.setJavaScriptEnabled(true);
        //!!!必须配置,否则第二次加载不到二维码
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        //!!!必须配置,否则白屏
        webSettings.setDomStorageEnabled(true);

        //设置自适应屏幕，两者合用
        webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小

        //缩放操作
        webSettings.setSupportZoom(true); //支持缩放，默认为true。是下面那个的前提。
        webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件

        //其他配置
        webSettings.setAllowFileAccess(true); //设置可以访问文件
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
        webSettings.setDefaultTextEncodingName("utf-8");//设置编码格式


        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                Log.i(TAG, "onPageStarted: " + url);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //页面加载完成
                Log.i(TAG, "onPageFinished: " + url);

                if (url.equals("https://eos.douyin.com/livesite/login")) {
                    //登录加载完成

                    //获取到二维码 base64 数据
                    WebUtils.getQrImg(view, new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String s) {

                            //展示二维码,并返回bitmap数据
                            Bitmap bitmap = WebUtils.showQr(imageView, s);

                            //保存二维码
                            WebUtils.saveQr(bitmap, new MediaScannerConnection.OnScanCompletedListener() {
                                @Override
                                public void onScanCompleted(String s, Uri uri) {
                                    //二维码保存成功后打开抖音
//                                    WebUtils.openDouyin();
                                }
                            });
                            Log.i(TAG, "onPageFinished: " + s);
                        }
                    });
                } else if (url.equals("https://eos.douyin.com/livesite")) {
                    //登录完成后,
                } else if (url.equals("https://eos.douyin.com/livesite/live/current")) {
                    //登录完成后,且所有信息加载完成
                    //自动化点击 "添加商品"
                    WebUtils.addCommodity(view, 0);
                }
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                Log.i(TAG, "shouldOverrideUrlLoading: " + view.getUrl());
                return super.shouldOverrideUrlLoading(view, request);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }

            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                //用于拦截
                return super.shouldInterceptRequest(view, request);
            }
        });
        webView.loadUrl(URL);
    }
}